﻿using System;
using System.IO;
using System.Linq;
using System.Text;
using System.Collections.Generic;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Json;

namespace TrainTicketsRobot
{
    [Serializable]
    class Passenger 
    {
        public Passenger() 
        {
            this.PasgerName = "";
            this.CardNumber = "";
            this.MobilePhone = "";
        }
        public string PasgerName;
        public string CardNumber;
        public string MobilePhone;
    }

    [Serializable]
    class PasgerList 
    {
        public PasgerList() 
        {
            PassengerList = null;
        }
        public Passenger[] PassengerList;
    }

    [Serializable]
    class FrmMainSetting
    {
        public FrmMainSetting() 
        {
            this.UserName = "";
            this.UserPwd = "";
            this.LoginInterval = 0;
            this.FromStation = "";
            this.ToStation = "";
            this.GoDate = "";
            this.GoTime = "";
            this.TrainNo = "";
            this.SearchInterval = 0;
            this.SetType = "";
        }
        public string UserName;
        public string UserPwd;
        public int LoginInterval;
        public string FromStation;
        public string ToStation;
        public string GoDate;
        public string GoTime;
        public string TrainNo;
        public int SearchInterval;
        public string SetType;
    }

    class FrmMainConfig 
    {
        public FrmMainConfig() 
        {
            this.frmMainSetting = new FrmMainSetting();
            this.pasgerList = new PasgerList();
        }
        public FrmMainSetting frmMainSetting;
        public PasgerList pasgerList;
    }

    class RobotConfig 
    {
        public FrmMainConfig LoadConfig()
        {
            try
            {
                if (!File.Exists(this.configPath) || !File.Exists(this.configPasgerPath)) 
                {
                    return null;
                }
                string strJson = File.ReadAllText(this.configPath);
                string strPasgerJson = File.ReadAllText(this.configPasgerPath);
                if (strJson == null || strJson == "" 
                    || strPasgerJson == null || strPasgerJson == "") 
                {
                    return null;
                }
                FrmMainConfig frmMainConfig = new FrmMainConfig();
                FrmMainSetting frmMainSetting = new FrmMainSetting();
                PasgerList pasgerList = new PasgerList();
                DataContractJsonSerializer dataConSer = new DataContractJsonSerializer(typeof(FrmMainSetting));
                using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(strJson)))
                {
                    frmMainSetting = dataConSer.ReadObject(ms) as FrmMainSetting;
                }

                DataContractJsonSerializer dataConSer2 = new DataContractJsonSerializer(typeof(PasgerList));
                using (MemoryStream ms2 = new MemoryStream(Encoding.UTF8.GetBytes(strPasgerJson)))
                {
                    pasgerList = dataConSer2.ReadObject(ms2) as PasgerList;
                }
                frmMainConfig.frmMainSetting = frmMainSetting;
                frmMainConfig.pasgerList = pasgerList;
                return frmMainConfig;
            }
            catch 
            {
                return null;
            }
        }
        public int SaveConfig(FrmMainConfig frmMainConfig)
        {
            try
            {
                if (!Directory.Exists(this.configDir))
                {
                    Directory.CreateDirectory(this.configDir);
                }

                DataContractJsonSerializer dataConSer = new DataContractJsonSerializer(frmMainConfig.frmMainSetting.GetType());
                string szJson = "";
                using (MemoryStream stream = new MemoryStream())
                {
                    dataConSer.WriteObject(stream, frmMainConfig.frmMainSetting);
                    szJson = Encoding.UTF8.GetString(stream.ToArray());
                }

                DataContractJsonSerializer dataConSer2 = new DataContractJsonSerializer(frmMainConfig.pasgerList.GetType());
                string szJson2 = "";
                using (MemoryStream stream2 = new MemoryStream())
                {
                    dataConSer2.WriteObject(stream2, frmMainConfig.pasgerList);
                    szJson2 = Encoding.UTF8.GetString(stream2.ToArray());
                }

                File.WriteAllText(this.configPath, szJson);
                File.WriteAllText(this.configPasgerPath, szJson2);
                return 0;
            }
            catch
            {
                return -1;
            }
        }

        private string configDir = "./config";
        private string configPath = "./config/winformSet.table";
        private string configPasgerPath = "./config/list.table";
    }
}
